package com.zmn.plat.business.impl.service.itempart;

import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.business.interfaces.service.itempart.ItemPartBService;
import com.zmn.plat.model.entity.service.itempart.ItemPart;
import com.zmn.plat.model.entity.service.itempart.ItemPartEditDIO;
import com.zmn.plat.model.entity.service.itempart.ItemPartQuery;
import com.zmn.plat.services.interfaces.service.itempart.ItemPartService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author tanbiao
 * @description
 * @since 2019/11/22 15:45
 * @modifier
 **/
@Service
public class ItemPartBServiceImpl implements ItemPartBService {

    @Resource
    ItemPartService itemPartService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setPartRelItem(ItemPartEditDIO dio) {
        /**
         * 删除原来的关系数据
         */
        itemPartService.deleteByItemId(dio.getItemId());
        /**
         * 批量插入数据
         */
        List<ItemPart> itemPartList = new ArrayList<>();
        if (CollectionUtil.isNotNullOrEmpty(dio.getPartSubCategoryIds())){
            dio.getPartSubCategoryIds().forEach(item->{
                String[] arr = item.split("_");
                if (arr.length == 2) {
                    ItemPart itemPart = new ItemPart();
                    itemPart.setItemId(dio.getItemId());
                    itemPart.setPartCategoryId(Integer.valueOf(arr[0]));
                    itemPart.setPartSubCategoryId(Integer.valueOf(arr[1]));
                    itemPart.setCreater(dio.getOperator());
                    itemPart.setUpdater(dio.getOperator());
                    itemPart.setCreateTime(new Date());
                    itemPart.setUpdateTime(new Date());
                    itemPartList.add(itemPart);
                }
            });
        }
        if (itemPartList.size()>0){
            itemPartService.insertBatch(itemPartList);
        }
    }
}
